package com.smart_campus.sys.common;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.smart_campus.sys.domain.UserInfo;
import com.smart_campus.sys.service.UserInfoService;
import com.smart_campus.sys.service.impl.UserInfoServiceImpl;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;

/**
 * @Author:Omega
 * @Date:2020/6/17 14:59
 */
public class identityVerify {
    /**身份证验证
     * @param username
     * @param identity
     */
    public static Boolean verification(String username, String identity) {
        String url = "https://eid.shumaidata.com/eid/check";
        String appCode = "78768d8b68634ec3bb95a82af41e6bac";

        Map<String, String> params = new HashMap<>();
        params.put("idcard", identity);
        params.put("name", username);
        Integer result = null;
        try {
            result = postForm(appCode, url, params);
            if (result==200){
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return false;
    }

    /**
     * 用到的HTTP工具包：okhttp 3.13.1
     * <dependency>
     * <groupId>com.squareup.okhttp3</groupId>
     * <artifactId>okhttp</artifactId>
     * <version>3.13.1</version>
     * </dependency>
     */

    public static Integer postForm(String appCode, String url, Map<String, String> params) throws IOException {
        OkHttpClient client = new OkHttpClient.Builder().build();
        FormBody.Builder formbuilder = new FormBody.Builder();
        Iterator<String> it = params.keySet().iterator();
        while (it.hasNext()) {
            String key = it.next();
            formbuilder.add(key, params.get(key));
        }
        FormBody body = formbuilder.build();
        Request request = new Request.Builder().url(url).addHeader("Authorization", "APPCODE " + appCode).post(body).build();
        Response response = client.newCall(request).execute();
        String result = response.body().string();
        System.out.println("身份证"+result);
        System.out.println("返回值是否为0"+response.code());
        if (response.code()==200){
            String str = result.substring(0, result.indexOf("\",\"description"));
            String str2 = str.substring(str.length()-1);
            System.out.println("res"+str2);
            if (!Objects.equals(str2, "1")){
                return 400;
            }
        }
        return response.code();
    }

}
